package com.cz.nongyetongv5.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.application.InitApplication;
import com.cz.nongyetongv5.bean.User;
import com.cz.nongyetongv5.custom.BlueLoadingProgress;

@SuppressLint("JavascriptInterface") public class MessageActivity extends Activity implements
		OnClickListener {

	private WebView wvStart;

	 

	private User user;

	private ViewGroup btnBack;

	private BlueLoadingProgress blpLoading = null;
	 

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		InitApplication initApplication = (InitApplication) getApplicationContext();

		initApplication.getActivities().add(this);

		setContentView(R.layout.activity_message);
		
		
	 

		wvStart = (WebView) findViewById(R.id.wvStart);

		 
		btnBack = (ViewGroup) findViewById(R.id.btnBack);
		btnBack.setOnClickListener(this);

		blpLoading = (BlueLoadingProgress) findViewById(R.id.blpLoading);
		blpLoading.setVisibility(View.GONE);

		user = (User) getIntent().getSerializableExtra("user");

		// ��������������

		// ����javascr
		wvStart.getSettings().setJavaScriptEnabled(true);

		wvStart.addJavascriptInterface(new MobInterp(), "mobinterp");
		wvStart.getSettings().setLoadsImagesAutomatically(true);
		 
		wvStart.setWebViewClient(new WebViewClient() {
			 public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)    
             { 
                  view.stopLoading();
                  view.clearView();
                  view.loadData("Cann't find data", "text/html", "utf-8");
                 // view.loadUrl("file:///android_asset/error/error500.html");
             }
			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {

				blpLoading.setVisibility(View.VISIBLE);

				super.onPageStarted(view, url, favicon);
			}

			@Override
			public void onPageFinished(WebView view, String url) {

				if (blpLoading != null) {

					blpLoading.stopLoading();
					blpLoading.setVisibility(View.GONE);

				}
				super.onPageFinished(view, url);
			}

		});
	   //web��ַ

		String url = getString(R.string.planurl);
             url+="/messages.html?data={\"user\":\""+user.getUsername()
            		 +"\",\"region\":\""+user.getKnowRegion()+"\"}";
	     Log.d("rafe",url);

		wvStart.loadUrl(url);
		  findViewById(R.id.btnMeanMore).setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				wvStart.reload();
			}
			  
			   
		  });
	 }

 
 

	@Override
	protected void onDestroy() {

		InitApplication initApplication = (InitApplication) getApplicationContext();

		initApplication.getActivities().remove(this);

		super.onDestroy();
	}

	@Override
	public void onClick(View v) {

		if (v == btnBack) {

			finish();

			return;
		}

		  
	}
	

 public class MobInterp{
	     
		 public MobInterp(){
			 
		 }
		 @JavascriptInterface
		 public void toDetail(String type,String pid,String title){
			 Intent intent=new Intent(MessageActivity.this,WebDetailActivity.class);
			   intent.putExtra("pid",pid);
			   intent.putExtra("type",type);
			   intent.putExtra("title",title);
			   intent.putExtra("user",user);
			   startActivity(intent);
		 }
		
	}
}
